{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cabb2fbc",
   "metadata": {},
   "source": [
    "## Batch:Total test data --> True mask and predicted mask pixels -> csv files\n",
    "\n",
    "Based on saved predicted mask files we are creating csv file : to save the true positive, true negative , false positive and false negative pixel wise scores\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3c7f9d92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "water_body_100_1.png\n",
      "[[  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " ...\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]]\n",
      "water_body_100_3.png\n",
      "[[  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " ...\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]]\n",
      "water_body_1029_10.png\n",
      "[[0 0 0 ... 0 0 0]\n",
      " [0 0 0 ... 0 0 0]\n",
      " [0 0 0 ... 0 0 0]\n",
      " ...\n",
      " [0 0 0 ... 0 0 0]\n",
      " [0 0 0 ... 0 0 0]\n",
      " [0 0 0 ... 0 0 0]]\n",
      "water_body_1037_16.png\n",
      "[[255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " ...\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]]\n",
      "water_body_1037_31.png\n",
      "[[  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " [  0   0   0 ... 255 255 255]\n",
      " ...\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]]\n",
      "water_body_1037_60.png\n",
      "[[255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " ...\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]\n",
      " [255 255 255 ... 255 255 255]]\n"
     ]
    }
   ],
   "source": [
    "# checking mask file contains 0 or 255 binary values\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "true_mask_folder=\"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch\"\n",
    "image_filenames = os.listdir(true_mask_folder)[:6]\n",
    "for filename in image_filenames:\n",
    "    # Load the true mask and predicted mask images\n",
    "    print(filename)\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    print(true_mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "379238ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch.csv\", index=False)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "923244b7",
   "metadata": {},
   "source": [
    "## batch1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c904f090",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch1/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch1\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch1.csv\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8750257e",
   "metadata": {},
   "source": [
    "## Batch 2:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "37fb3a3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch2/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch2\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch2.csv\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b60ee7bb",
   "metadata": {},
   "source": [
    "## batch 3:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "484757e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch3/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch3\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch3.csv\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f6a6886",
   "metadata": {},
   "source": [
    "## Batch 4:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e5f8de49",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch4/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch4\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch4.csv\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3745c386",
   "metadata": {},
   "source": [
    "## Batch 5:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9fd194be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch5/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch5\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch5.csv\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d82f35df",
   "metadata": {},
   "source": [
    "## Batch 6:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7b06af1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# Define paths to the true mask and predicted mask folders\n",
    "true_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/data/test_split/batch6/masks\"\n",
    "predicted_mask_folder = \"C:/Users/rajes/OneDrive/Desktop/dataset/results_transdeeplab/best_weights/batch6\"\n",
    "\n",
    "# Initialize variables to store the counts of true positive, false positive, true negative,\n",
    "# and false negative water pixels\n",
    "true_positive_water_pixels = 0\n",
    "false_positive_water_pixels = 0\n",
    "true_negative_water_pixels = 0\n",
    "false_negative_water_pixels = 0\n",
    "\n",
    "# Create empty lists to store the data for each image\n",
    "filenames = []\n",
    "true_water_pixels = []\n",
    "true_black_pixels = []\n",
    "predicted_water_pixels = []\n",
    "predicted_black_pixels = []\n",
    "true_positive_pixels = []\n",
    "false_positive_pixels = []\n",
    "true_negative_pixels = []\n",
    "false_negative_pixels = []\n",
    "\n",
    "# Loop through each file in the true mask folder\n",
    "for filename in os.listdir(true_mask_folder):\n",
    "    # Load the true and predicted masks\n",
    "    true_mask = cv2.imread(os.path.join(true_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "    predicted_mask = cv2.imread(os.path.join(predicted_mask_folder, filename), cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "    # Count the number of water and black pixels in the true mask\n",
    "    true_water_pixels_count = np.count_nonzero(true_mask == 255)\n",
    "    true_black_pixels_count = np.count_nonzero(true_mask == 0)\n",
    "\n",
    "    # Count the number of water and black pixels in the predicted mask\n",
    "    predicted_water_pixels_count = np.count_nonzero(predicted_mask == 255)\n",
    "    predicted_black_pixels_count = np.count_nonzero(predicted_mask == 0)\n",
    "\n",
    "    # Calculate the number of true positive, false positive, true negative, and false negative\n",
    "    # water pixels\n",
    "    for i in range(true_mask.shape[0]):\n",
    "        for j in range(true_mask.shape[1]):\n",
    "            if true_mask[i, j] == 255 and predicted_mask[i, j] == 255:\n",
    "                true_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 255:\n",
    "                false_positive_water_pixels += 1\n",
    "            elif true_mask[i, j] == 0 and predicted_mask[i, j] == 0:\n",
    "                true_negative_water_pixels += 1\n",
    "            elif true_mask[i, j] == 255 and predicted_mask[i, j] == 0:\n",
    "                false_negative_water_pixels += 1\n",
    "\n",
    "    # Add the data for this image to the lists\n",
    "    filenames.append(filename)\n",
    "    true_water_pixels.append(true_water_pixels_count)\n",
    "    true_black_pixels.append(true_black_pixels_count)\n",
    "    predicted_water_pixels.append(predicted_water_pixels_count)\n",
    "    predicted_black_pixels.append(predicted_black_pixels_count)\n",
    "    true_positive_pixels.append(true_positive_water_pixels)\n",
    "    false_positive_pixels.append(false_positive_water_pixels)\n",
    "    true_negative_pixels.append(true_negative_water_pixels)\n",
    "    false_negative_pixels.append(false_negative_water_pixels)\n",
    "\n",
    "    # Reset the variables for the next iteration\n",
    "    true_positive_water_pixels = 0\n",
    "    false_positive_water_pixels = 0\n",
    "    true_negative_water_pixels = 0\n",
    "    false_negative_water_pixels = 0\n",
    "\n",
    "# Create a DataFrame to store the data\n",
    "data = pd.DataFrame({\n",
    "    \"filename\": filenames,\n",
    "    \"true_water_pixels\": true_water_pixels,\n",
    "    \"true_black_pixels\": true_black_pixels,\n",
    "    \"predicted_water_pixels\": predicted_water_pixels,\n",
    "    \"predicted_black_pixels\": predicted_black_pixels,\n",
    "    \"true_positive_pixels\":true_positive_pixels,\n",
    "    \"false_positive_pixels\": false_positive_pixels,\n",
    "    \"true_negative_pixels\": true_negative_pixels,\n",
    "    \"false_negative_pixels\": false_negative_pixels\n",
    "    })\n",
    "data.to_csv(\"C:/Users/rajes/OneDrive/Desktop/dataset/csv_files/transdeeplab/new_batch6.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b4a0af9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
